/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-06-13
*/
package com.rzico.order.controller.admin;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rzico.annotation.Log;
import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.basics.entity.Enterprise;
import com.rzico.basics.service.EnterpriseService;
import com.rzico.core.entity.SysMch;
import com.rzico.core.service.SysMchService;
import com.rzico.entity.Filter;
import com.rzico.entity.Pageable;
import com.rzico.entity.PageResult;
import com.rzico.order.entity.OrderReview;
import com.rzico.order.service.OrderReviewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.Api;
import com.rzico.util.StringUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
import java.util.Map;

/**
 * 订单评论控制层
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2020-06-13
 */
@Api(description = "订单评论接口")
@RestController
@RequestMapping("/admin/orderReview")
public class OrderReviewAdminController extends BaseController {

    @Autowired
    private OrderReviewService orderReviewService;

    @Autowired
    private EnterpriseService enterpriseService;

    @Autowired
    private SysMchService sysMchService;

    /**
     * 分页查询订单评论
     *
     * @return
    */
    @ApiOperation("分页查询订单评论")
    @GetMapping("/list")
    @ApiImplicitParams({@ApiImplicitParam(name = "startDate", value = "开始时间", dataType = "String", paramType = "query") ,
            @ApiImplicitParam(name = "endDate", value = "结束时间", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "star", value = "星级(4,5.好评,3.中评,1,2.差评)", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "keyword", value = "商品/订单号/手机号/取餐号/收货人/地址", dataType = "String", paramType = "query")
    })
    public CommResult<OrderReview> list(String startDate, String endDate, Integer star,String keyword, Pageable pageable) {
        Map<String, Object> params = new HashMap<String, Object>();
        params = buildSortField(params, pageable);
        if (StringUtils.isNotEmpty(startDate)){
            params.put("startDate", startDate);
        }
        if (StringUtils.isNotEmpty(endDate)){
            params.put("endDate", endDate);
        }

        if (star!=null){
            params.put("star", star);
        }

        if (keyword!=null){
            params.put("keyword", keyword);
        }


        Enterprise enterprise = enterpriseService.getCurrent();
        SysMch sysMch = sysMchService.selectByPrimaryKey(enterprise.getMchId());
        if (!sysMch.getMchType().equals(0))  {
            params.put("enterpriseId", enterprise.getId());
        }


//        Enterprise enterprise = enterpriseService.getCurrent();
//        if (enterprise!=null) {
//            params.put("enterpriseId", enterprise.getId());
//        } else {
//            return CommResult.error("没有开通企业");
//        }

        Page<Object> startPage = PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<OrderReview> list = orderReviewService.selectList(params);
        PageResult<OrderReview> pageResult = new PageResult<OrderReview>(list, startPage.getTotal(), pageable);
        return CommResult.success(pageResult);
    }


    /**
     * 批量删除订单评论
     *
     * @param ids
     * @return
     */
    @Log(desc = "批量删除订单评论", type = Log.LOG_TYPE.DEL)
    @ApiOperation("批量删除订单评论,ids用逗号拼接")
    @PostMapping("/del/{ids}")
    public CommResult<OrderReview> del(@PathVariable String ids) {


        try {
            orderReviewService.deleteByIds(ids.split(","));
        } catch (Exception e) {
            return CommResult.error(e.getMessage());
        }
        return CommResult.success();
    }


}
